//
//  PersonView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/16.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol PersonViewDelegate {
    // 保存
    func personview_save(item:CallbackBean)
    // 删除
    func personview_delete(person:PersonExpenseInfo)
}

class PersonView: UIView {
    
    // 常量
    private let titles = ["抬头","税号"]
    private let placeholders = ["必填，个人姓名","必填，个人身份证号"]
    // 存储
    private var titleLables = [UILabel]()
    private var holderViews = [TextFieldOffsetPlaceHolder]()
    
    // 回调
    public var delegate:PersonViewDelegate?
    
    // 赋值
    public var personExpenseInfo:PersonExpenseInfo? {
        didSet {
            holderViews[0].text = personExpenseInfo?.title
            holderViews[1].text = personExpenseInfo?.duty
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        // 记录最后一个view高度
        var tempHeight:CGFloat = 0
        (0..<titles.count).forEach { (i) in
            let labelY = MARGIN + CGFloat(i) * MARGIN70
            // 标题
            let label = titleLables[i]
            label.text = titles[i]
            label.textColor = COLOR_NICE
            label.font = UIFont.systemFont(ofSize: 17)
            label.frame = CGRect(x: MARGIN, y: labelY, width: MARGIN * 4.8, height: MARGIN30)
            addSubview(label)
            // 输入
            let input = holderViews[i]
            input.frame = CGRect(x: MARGIN, y: labelY, width: SCREEN_WIDTH, height: MARGIN30)
            // 下划线
            let line = UIView()
            line.backgroundColor = COLOR_LINE
            line.frame = CGRect(x: MARGIN, y: input.frame.maxY + MARGIN, width: SCREEN_WIDTH - MARGIN * 2, height: 1)
            addSubview(line)
            // 最后一个view
            if i == titles.count - 1 {
                tempHeight = line.frame.maxY
            }
        }
        // 确认修改按钮
        let saveBtn = UIButton(title: "保存", name: "button_orange",radius: 5)
        saveBtn.frame = CGRect(x: MARGIN, y: tempHeight + MARGIN30, width: SCREEN_WIDTH - MARGIN * 2, height: MARGIN50)
        addSubview(saveBtn)
        // 确认修改按钮 添加点击事件
        saveBtn.addTarget(self, action: #selector(clickSaveItem), for: .touchUpInside)
        // 是否显示删除按钮
        if personExpenseInfo != nil  {
            let deleteLabel = titleLables[titles.count]
            deleteLabel.text = "删除该报销"
            deleteLabel.textColor = UIColor.orange
            deleteLabel.font = UIFont.systemFont(ofSize: 14)
            deleteLabel.textAlignment = .center
            deleteLabel.frame = CGRect(x: 0, y: saveBtn.frame.maxY + MARGIN20, width: frame.width, height: MARGIN)
            addSubview(deleteLabel)
            // 添加点击事件
            deleteLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickDeleteItem)))
        }
    }
    
    @objc func clickSaveItem() -> Void {
        let bean = CallbackBean(firstIn: holderViews[0].text, secondIn: holderViews[1].text)
        delegate?.personview_save(item: bean)
    }
    
    @objc func clickDeleteItem() -> Void {
        guard let person = personExpenseInfo else {
            return
        }
        delegate?.personview_delete(person: person)
    }
}

extension PersonView {
    
    private func init_views() -> Void {
        (0..<titles.count).forEach { (i) in
            let input = TextFieldOffsetPlaceHolder()
            input.placeholder = placeholders[i]
            input.textColor = UIColor.lightGray
            input.font = UIFont.systemFont(ofSize: 13)
            input.setValue(MARGIN * 5, forKey: "paddingLeft")
            holderViews.append(input)
            addSubview(input)
        }
        // 说明
        (0...titles.count).forEach { (i) in
            titleLables.append(UILabel())
        }
    }
}
